
#define _DEFAULT_SOURCE /* needed for usleep() */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#define SDL_MAIN_HANDLED /*To fix SDL's "undefined reference to WinMain" issue*/
#include <SDL2/SDL.h>
// #include "lvgl/lvgl.h"
// #include "lvgl/examples/lv_examples.h"
// #include "lv_examples/lv_demo.h"
// #include "lv_drivers/display/monitor.h"
// #include "lv_drivers/indev/mouse.h"
// #include "lv_drivers/indev/keyboard.h"
// #include "lv_drivers/indev/mousewheel.h"

#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>


#define DISP_BUF_SIZE (128 * 800)   //显示缓冲区
#define LOGIN 1     //登陆
#define REGISTER 0  //注册

lv_obj_t *  contanier_start = NULL; //作为开始界面容器
lv_obj_t *  contanier_login = NULL; //作为登陆界面容器
lv_obj_t *  contanier_register = NULL; //作为注册界面
lv_obj_t *  contanier_mode = NULL; // 模式选择界面
lv_obj_t *  contanier_home = NULL; //进入房间界面
lv_obj_t *  contanier_play = NULL; //下棋界面
lv_obj_t * password;               //创建密码对象
lv_obj_t * user_name;              //创建用户名对象
lv_obj_t * btn3,*btn4;             //btn3注册按钮，btn登陆按钮，为了使用键盘时隐藏该按钮，设为全局
int client_socket;                 //用户链接后返回的套接字描述符

static void hal_init(void);
static int tick_thread(void *data);
void test();
void test(){
      /*创建再来一局按钮*/
    lv_obj_t *btn_again=lv_btn_create(contanier_result);
    lv_obj_set_size(btn_again,150,50);
    lv_obj_align(btn_again,LV_ALIGN_CENTER,0,-30);
    lv_obj_t * again=lv_label_create(btn_again);
    lv_label_set_text(again,"again");
    lv_obj_center(again);

    /*创建换桌按钮*/
    lv_obj_t *btn_change=lv_btn_create(contanier_result);
    lv_obj_set_size(btn_change,150,50);
    lv_obj_align(btn_change,LV_ALIGN_CENTER,0,40);
    lv_obj_t * change=lv_label_create(btn_change);
    lv_label_set_text(change,"changetable");  //Substitution：替代, 取代, 替换, 代换, 置换, 代用
    lv_obj_center(change);
}
int main(int argc,char **argv){
  lv_init();
  hal_init();
  //!这↓里开始改
  test();
  while(1){
    lv_timer_handler();
    usleep(5*1000);
  }
  return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}


static void hal_init(void){
  monitor_init();
  SDL_CreateThread(tick_thread,"tick",NULL);
  /*Create a display buffer*/
  static lv_disp_draw_buf_t disp_buf1;
  static lv_color_t buf1_1[MONITOR_HOR_RES*100];
  static lv_color_t buf1_2[MONITOR_HOR_RES*100];
  lv_disp_draw_buf_init(&disp_buf1,buf1_1,buf1_2,MONITOR_HOR_RES*100);
  /*Create a display*/
  static lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  disp_drv.draw_buf=&disp_buf1;
  disp_drv.flush_cb=monitor_flush;
  disp_drv.hor_res=MONITOR_HOR_RES;
  disp_drv.ver_res=MONITOR_VER_RES;
  disp_drv.antialiasing=1;

  lv_disp_t * disp=lv_disp_drv_register(&disp_drv);

  lv_theme_t * th=lv_theme_default_init(disp,lv_palette_main(LV_PALETTE_BLUE),lv_palette_main(LV_PALETTE_RED),LV_THEME_DEFAULT_DARK,LV_FONT_DEFAULT);
  lv_disp_set_theme(disp,th);

  lv_group_t * g=lv_group_create();
  lv_group_set_default(g);

  /* Add the mouse as input device
   * Use the 'mouse' driver which reads the PC's mouse*/
  mouse_init();
  static lv_indev_drv_t indev_drv_1;
  lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
  indev_drv_1.type=LV_INDEV_TYPE_POINTER;

  /*This function will be called periodically (by the library) to get the mouse position and state*/
  indev_drv_1.read_cb=mouse_read;
  lv_indev_t *mouse_indev=lv_indev_drv_register(&indev_drv_1);

  keyboard_init();
  static lv_indev_drv_t indev_drv_2;
  lv_indev_drv_init(&indev_drv_2); /*Basic initialization*/
  indev_drv_2.type=LV_INDEV_TYPE_KEYPAD;
  indev_drv_2.read_cb=keyboard_read;
  lv_indev_t *kb_indev=lv_indev_drv_register(&indev_drv_2);
  lv_indev_set_group(kb_indev,g);
  mousewheel_init();
  static lv_indev_drv_t indev_drv_3;
  lv_indev_drv_init(&indev_drv_3); /*Basic initialization*/
  indev_drv_3.type=LV_INDEV_TYPE_ENCODER;
  indev_drv_3.read_cb=mousewheel_read;

  lv_indev_t * enc_indev=lv_indev_drv_register(&indev_drv_3);
  lv_indev_set_group(enc_indev,g);

  /*Set a cursor for the mouse*/
  LV_IMG_DECLARE(mouse_cursor_icon); /*Declare the image file.*/
  lv_obj_t * cursor_obj=lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
  lv_img_set_src(cursor_obj,&mouse_cursor_icon);           /*Set the image source*/
  lv_indev_set_cursor(mouse_indev,cursor_obj);             /*Connect the image  object to the driver*/
}

/**
 * A task to measure the elapsed time for LVGL
 * @param data unused
 * @return never return
 */
static int tick_thread(void *data){
  (void)data;
  while(1){
    SDL_Delay(5);
    lv_tick_inc(5); /*Tell LittelvGL that 5 milliseconds were elapsed*/
  }
  return 0;
}